#include "tabel.h"
#include <QHBoxLayout>
#include <QDebug>

Tabel::Tabel(QWidget *parent) : QMainWindow(parent),
    maxTime(60), maxTemp(40), maxHum(100), maxIllum(1000)
{
    // 创建主布局
    QWidget *centralWidget = new QWidget(this);
    QHBoxLayout *mainLayout = new QHBoxLayout(centralWidget);
    setCentralWidget(centralWidget);

    // 初始化图表
    customPlot = new QCustomPlot(this);
    mainLayout->addWidget(customPlot);

    // ==== 科技风格背景设置 ====
    customPlot->setBackground(QBrush(QColor(20, 20, 35)));

    // 网格线样式
    QPen gridPen(QColor(40, 40, 60, 100), 0.5, Qt::DotLine);
    customPlot->xAxis->grid()->setPen(gridPen);
    customPlot->yAxis->grid()->setPen(gridPen);
    customPlot->yAxis2->grid()->setPen(gridPen);

    // 坐标轴标签样式
    QColor axisLabelColor(180, 180, 220);
    customPlot->xAxis->setLabelColor(axisLabelColor);
    customPlot->yAxis->setLabelColor(axisLabelColor);
    customPlot->yAxis2->setLabelColor(axisLabelColor);
    customPlot->xAxis->setLabelFont(QFont("Segoe UI", 9, QFont::Medium));
    customPlot->yAxis->setLabelFont(QFont("Segoe UI", 9, QFont::Medium));
    customPlot->yAxis2->setLabelFont(QFont("Segoe UI", 9, QFont::Medium));

    // 刻度标签样式
    QColor tickLabelColor(200, 200, 230);
    customPlot->xAxis->setTickLabelColor(tickLabelColor);
    customPlot->yAxis->setTickLabelColor(tickLabelColor);
    customPlot->yAxis2->setTickLabelColor(tickLabelColor);
    customPlot->xAxis->setTickLabelFont(QFont("Segoe UI", 8));
    customPlot->yAxis->setTickLabelFont(QFont("Segoe UI", 8));
    customPlot->yAxis2->setTickLabelFont(QFont("Segoe UI", 8));

    // 坐标轴线样式
    QPen axisPen(QColor(80, 80, 120));
    axisPen.setWidth(1);
    customPlot->xAxis->setBasePen(axisPen);
    customPlot->yAxis->setBasePen(axisPen);
    customPlot->yAxis2->setBasePen(axisPen);

    // ==== 创建三条数据曲线 ====
    tempGraph = customPlot->addGraph();
    humGraph = customPlot->addGraph();
    illumGraph = customPlot->addGraph();

    // 设置曲线样式
    QPen pen;
    pen.setWidth(1.8);

    // 温度曲线(红色)
    pen.setColor(QColor(255, 50, 50));
    tempGraph->setPen(pen);
    tempGraph->setName("温度(℃)");
    // 散点大小通过QCPScatterStyle设置
    tempGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 4));

    // 湿度曲线(蓝色)
    pen.setColor(QColor(50, 150, 255));
    humGraph->setPen(pen);
    humGraph->setName("湿度(%)");
    humGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 4));

    // 光照曲线(橙色)
    pen.setColor(QColor(255, 180, 50));
    illumGraph->setPen(pen);
    illumGraph->setName("光照(Lux)");
    illumGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 4));

    // ==== 高级发光效果实现 ====
    // 创建发光效果的阴影曲线
    QCPGraph *glowGraphTemp = customPlot->addGraph();
    glowGraphTemp->setPen(QPen(QColor(255, 50, 50, 60), 8));
    glowGraphTemp->setBrush(Qt::NoBrush);

    QCPGraph *glowGraphHum = customPlot->addGraph();
    glowGraphHum->setPen(QPen(QColor(50, 150, 255, 60), 8));
    glowGraphHum->setBrush(Qt::NoBrush);

    QCPGraph *glowGraphIllum = customPlot->addGraph();
    glowGraphIllum->setPen(QPen(QColor(255, 180, 50, 60), 8));
    glowGraphIllum->setBrush(Qt::NoBrush);

    // 启用右侧Y轴(用于光照)
    customPlot->yAxis2->setVisible(true);

    // 设置坐标轴标签
    customPlot->xAxis->setLabel("时间(秒)");
    customPlot->yAxis->setLabel("温度/湿度");
    customPlot->yAxis2->setLabel("光照强度");

    // 设置坐标轴范围
    customPlot->xAxis->setRange(0, maxTime);
    customPlot->yAxis->setRange(0, 100);        // 固定0-100范围
    customPlot->yAxis2->setRange(0, maxIllum);   // 独立范围

    // 配置光照使用右侧Y轴
    illumGraph->setValueAxis(customPlot->yAxis2);

    // ==== 图例高级配置 (兼容旧版API) ====
    customPlot->legend->setVisible(true);
    customPlot->legend->setBrush(QBrush(QColor(35, 35, 50, 200)));
    customPlot->legend->setTextColor(QColor(200, 200, 220));
    customPlot->legend->setFont(QFont("Segoe UI", 9));

    // 兼容旧版API的图例位置设置
    // 使用左上角作为初始位置（右上角在旧版中实现复杂）
    customPlot->legend->setBrush(QBrush(QColor(35, 35, 50, 200)));

    // 启用缩放和拖拽
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

    // 抗锯齿效果
    customPlot->setAntialiasedElements(QCP::aeAll);

    // 设置图层顺序确保发光效果在实线下方
    customPlot->addLayer("glow", customPlot->layer("axes"), QCustomPlot::limBelow);
    glowGraphTemp->setLayer("glow");
    glowGraphHum->setLayer("glow");
    glowGraphIllum->setLayer("glow");

    // 设置发光曲线的数据来源（在添加数据点后更新）
    glowGraphTemp->setData(tempGraph->data());
    glowGraphHum->setData(humGraph->data());
    glowGraphIllum->setData(illumGraph->data());
}

void Tabel::addDataPoint(double time, double temp, double hum, double illum)
{
    // 添加新数据
    timeData.append(time);
    tempData.append(temp);
    humData.append(hum);
    illumData.append(illum);

    // 更新最大值
    maxTime = qMax(maxTime, time);
    maxTemp = qMax(maxTemp, temp + 5);
    maxIllum = qMax(maxIllum, illum + 50);

    // 设置数据
    tempGraph->setData(timeData, tempData);
    humGraph->setData(timeData, humData);
    illumGraph->setData(timeData, illumData);

    // 更新坐标轴范围
    const int visibleTimeRange = 60; // 显示60秒数据
    double endTime = time;
    double startTime = qMax(0.0, endTime - visibleTimeRange);
    customPlot->xAxis->setRange(startTime, endTime);

    // 温湿度固定0-100范围
    customPlot->yAxis->setRange(0, 100);

    // 光照动态调整
    double illumMin = 0;
    double illumMax = qMax(100.0, maxIllum);
    double padding = illumMax * 0.1;
    customPlot->yAxis2->setRange(illumMin, illumMax + padding);

    // 设置光照刻度间距
    QSharedPointer<QCPAxisTicker> ticker(new QCPAxisTicker);
    ticker->setTickCount(5); // 5个主要刻度
    customPlot->yAxis2->setTicker(ticker);

    // 重绘图表
    customPlot->replot();
}

